package com.waymap.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageInfo;
import com.waymap.Thread.WmsThread;
import com.waymap.common.dataGridResult.DataGridResult;
import com.waymap.domain.*;
import com.waymap.service.*;
import com.waymap.utils.mq.MsgProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.waymap.common.Constant;
import com.waymap.mapper.TransactionsMapper;
import com.waymap.utils.AuthonTokenUtil;
import com.waymap.utils.CommonEnum;
import com.waymap.utils.ExcelData;
import com.waymap.utils.ExcelUtils;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @author vincent mai
 * 交易管理
 */

@Api(value = "WMS系统", tags = {"WMS-交易管理"})
@CrossOrigin
@RestController
@RequestMapping("/transasctions")
public class TransactionController {

    @Autowired
    private TransactionsService transactionsService;
    @Autowired
    private ItemInventoryService itemInventoryService;
    @Autowired
    private ItemService itemService;
    @Autowired
    private NSOperateDataService nsOperateDataService;
    @Autowired
    private TransactionInventoryDetailsService transactionInventoryDetailsService;
    @Autowired
    private UserService userService;
    @Resource
    private TransactionsMapper transactionsMapper;
    @Autowired
    private MsgProducer producer;

    @ApiOperation(value = "单据清单数据")
    @RequestMapping(value = "/getTranidList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Map> getTranidList(
            @ApiParam(value = "单据类型") @RequestParam(required = false) String transactionType,
            @ApiParam(value = "当前用户token") @RequestParam(required = false) String token,
            @ApiParam(value = "用户Id") @RequestParam(required = false) String userId,
            @ApiParam(value = "入口") @RequestParam(required = false) String entry) {

        Map<String, Object> authonMap = AuthonTokenUtil.checkToken(token, userId);
        System.out.println(authonMap);
        User currentUser;
        if ((authonMap != null) && ((Constant.NO).equals(authonMap.get("isAvailable")))) {
            return ResponseEntity.ok(authonMap);
        } else {
            currentUser = userService.getUserInfoByToken(token);
            if (currentUser == null) {
                authonMap.put("isAvailable", Constant.NO);
                authonMap.put("errorCode", Constant.TOKEN_ERRCODE_EXPIRE);
                authonMap.put("msg", "签名过期");
                return ResponseEntity.ok(authonMap);
            }
        }

        Map<String, Object> param = new HashMap(16);
        param.put("transactionType", transactionType);
        param.put("accountId", currentUser.getAccountId());

        // 只有TRACKING_NUMBER类型的单据才会标识从哪类单据创建的
        if ("TRACKING_NUMBER".equals(transactionType)) {
            param.put("createFromType", entry);
            if ("TO".equals(entry)) {
                param.put("toLocationId", currentUser.getLocationId());
            } else if ("PO".equals(entry)) {
                param.put("locationId", currentUser.getLocationId());
            }
        } else {
            // 当界面为调拔入库时，取入库仓库为当前用户的仓库的单
            if ("TO".equals(transactionType) && "IN".equals(entry)) {
                param.put("toLocationId", currentUser.getLocationId());
            } else if ("TO_WAVE".equals(transactionType) && "OUT".equals(entry)) {
                param.put("locationId", currentUser.getLocationId());
            } else {
                param.put("locationId", currentUser.getLocationId());
            }
        }


        List<Map<String, Object>> list = transactionsService.getTranidList(param);


        Map<String, Object> map = new HashMap(16);
        if (null != list && list.size() > 0) {
            map.put("result", list);
            map.put("status", CommonEnum.value("eSuccess"));
            map.put("msg", CommonEnum.value("eSMessage"));
            return ResponseEntity.ok(map);
        } else {
            map.put("status", CommonEnum.value("eNotFind"));
            map.put("msg", CommonEnum.value("notFind"));
            return ResponseEntity.ok(map);
        }
    }

    @ApiOperation(value = "获取单据状态")
    @RequestMapping(value = "/getTranidStatus", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Map> getTranidStatus(
            @ApiParam(value = "单号") @RequestParam(required = false) String tranid,
            @ApiParam(value = "当前用户token") @RequestParam(required = false) String token,
            @ApiParam(value = "用户Id") @RequestParam(required = false) String userId) {
        Map<String, Object> authonMap = AuthonTokenUtil.checkToken(token, userId);
        System.out.println(authonMap);
        User currentUser;
        String accountId = null;
        if ((authonMap != null) && ((Constant.NO).equals(authonMap.get("isAvailable")))) {
            return ResponseEntity.ok(authonMap);
        } else {
            currentUser = userService.getUserInfoByToken(token);
            accountId = currentUser.getAccountId();
            if (currentUser == null) {
                authonMap.put("isAvailable", Constant.NO);
                authonMap.put("errorCode", Constant.TOKEN_ERRCODE_EXPIRE);
                authonMap.put("msg", "You have been logged out");
                return ResponseEntity.ok(authonMap);
            }
        }
        Map<String, Object> map = transactionsService.getTranidStatus(tranid, accountId);
        return ResponseEntity.ok(map);
    }

    @ApiOperation(value = "批次清单")
    @RequestMapping(value = "/getTransationBinName", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Map> getTransationBinName(@ApiParam(value = "商品id") @RequestParam String itemId,
                                                    @ApiParam(value = "当前用户token") @RequestParam(required = false) String token,
                                                    @ApiParam(value = "用户Id") @RequestParam(required = false) String userId,
                                                    @ApiParam(value = "入口") @RequestParam(required = false) String entry
    ) {
        Map<String, Object> authonMap = AuthonTokenUtil.checkToken(token, userId);
        System.out.println(authonMap);
        User currentUser = null;
        String accountId = null;
        if ((authonMap != null) && ((Constant.NO).equals(authonMap.get("isAvailable")))) {
            return ResponseEntity.ok(authonMap);
        } else {
            currentUser = userService.getUserInfoByToken(token);
            if (currentUser == null) {
                authonMap.put("isAvailable", Constant.NO);
                authonMap.put("errorCode", Constant.TOKEN_ERRCODE_EXPIRE);
                authonMap.put("msg", "签名过期");
                return ResponseEntity.ok(authonMap);
            }
            accountId = currentUser.getAccountId();
        }
        Map<String, Object> param = new HashMap(16);
        param.put("accountId", accountId);
        param.put("itemId", itemId);
        param.put("locationId", currentUser.getLocationId());
        //param.put("quantity", 0);
        List<Map<String, Object>> itemInventorylist = itemInventoryService.selectByItemId(param);
        Map<String, Object> map = new HashMap(16);
        map.put("result", itemInventorylist);
        map.put("status", CommonEnum.value("eSuccess"));
        map.put("msg", CommonEnum.value("eSMessage"));
        return ResponseEntity.ok(map);
    }

    @ApiOperation(value = "获取TransactionItemInventoryDetails表对应库位的数据")
    @RequestMapping(value = "/getTransactionItemInventoryDetails", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Map> getTransactionItemInventoryDetails(@ApiParam(value = "商品id") @RequestParam String trainid,
                                                                  @ApiParam(value = "商品id") @RequestParam String itemId,
                                                                  @ApiParam(value = "当前用户token") @RequestParam(required = false) String token,
                                                                  @ApiParam(value = "用户Id") @RequestParam(required = false) String userId,
                                                                  @ApiParam(value = "入口") @RequestParam(required = false) String entry,
                                                                  @ApiParam(value = "入口") @RequestParam(required = false) String binNumber
    ) {
        Map<String, Object> authonMap = AuthonTokenUtil.checkToken(token, userId);
        System.out.println(authonMap);
        User currentUser = null;
        String accountId = null;
        if ((authonMap != null) && ((Constant.NO).equals(authonMap.get("isAvailable")))) {
            return ResponseEntity.ok(authonMap);
        } else {
            currentUser = userService.getUserInfoByToken(token);
            if (currentUser == null) {
                authonMap.put("isAvailable", Constant.NO);
                authonMap.put("errorCode", Constant.TOKEN_ERRCODE_EXPIRE);
                authonMap.put("msg", "签名过期");
                return ResponseEntity.ok(authonMap);
            }
            accountId = currentUser.getAccountId();
        }
        TransactionItemInventoryDetails transactionItemInventoryDetails = new TransactionItemInventoryDetails();
        transactionItemInventoryDetails.setTransactionId(trainid);
        transactionItemInventoryDetails.setItemId(Integer.valueOf(itemId));
        transactionItemInventoryDetails.setAccountId(accountId);
//        transactionItemInventoryDetails.setBinNumber(binNumber);
        transactionItemInventoryDetails.setStatus("PENDING");
        List<TransactionItemInventoryDetails> transactionItemInventoryDetails1 = transactionInventoryDetailsService.selectTrans(transactionItemInventoryDetails);
        Map<String, Object> map = new HashMap(16);
        map.put("result", transactionItemInventoryDetails1);
        map.put("status", CommonEnum.value("eSuccess"));
        map.put("msg", CommonEnum.value("eSMessage"));
        return ResponseEntity.ok(map);
    }

    @ApiOperation(value = "销售出库清单")
    @RequestMapping(value = "/getTxSubmitDetail", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Map> getTxSubmitDetail(@ApiParam(value = "单号") @RequestParam String tranid,
                                                 @ApiParam(value = "单据类型") @RequestParam(required = false) String transactionType,
                                                 @ApiParam(value = "当前用户token") @RequestParam(required = false) String token,
                                                 @ApiParam(value = "用户Id") @RequestParam(required = false) String userId
    ) {
        Map<String, Object> authonMap = AuthonTokenUtil.checkToken(token, userId);
        System.out.println(authonMap);
        User currentUser = null;
        String accountId = null;
        if ((authonMap != null) && ((Constant.NO).equals(authonMap.get("isAvailable")))) {
            return ResponseEntity.ok(authonMap);
        } else {
            currentUser = userService.getUserInfoByToken(token);
            if (currentUser == null) {
                authonMap.put("isAvailable", Constant.NO);
                authonMap.put("errorCode", Constant.TOKEN_ERRCODE_EXPIRE);
                authonMap.put("msg", "签名过期");
                return ResponseEntity.ok(authonMap);
            }
            accountId = currentUser.getAccountId();
        }
        Map<String, Object> map = transactionsService.getTxSubmitDetail(tranid, transactionType, accountId);
        return ResponseEntity.ok(map);
    }


    @ApiOperation(value = "出库清单")
    @RequestMapping(value = "/getTransationWave", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Map> getTransationWave(@ApiParam(value = "单号") @RequestParam String tranid,
                                                 @ApiParam(value = "单据类型") @RequestParam(required = false) String transactionType,
                                                 @ApiParam(value = "当前用户token") @RequestParam(required = false) String token,
                                                 @ApiParam(value = "用户Id") @RequestParam(required = false) String userId,
                                                 @ApiParam(value = "入口") @RequestParam(required = false) String entry
    ) {
        Map<String, Object> authonMap = AuthonTokenUtil.checkToken(token, userId);
        System.out.println(authonMap);
        User currentUser = null;
        String accountId = null;
        if ((authonMap != null) && ((Constant.NO).equals(authonMap.get("isAvailable")))) {
            return ResponseEntity.ok(authonMap);
        } else {
            currentUser = userService.getUserInfoByToken(token);
            if (currentUser == null) {
                authonMap.put("isAvailable", Constant.NO);
                authonMap.put("errorCode", Constant.TOKEN_ERRCODE_EXPIRE);
                authonMap.put("msg", "签名过期");
                return ResponseEntity.ok(authonMap);
            }
            accountId = currentUser.getAccountId();
        }
        Map<String, Object> map = transactionsService.selectToWaveList(tranid, transactionType, accountId);
        return ResponseEntity.ok(map);
    }

    @ApiOperation(value = "单据明细清单")
    @RequestMapping(value = "/getTransationDetailList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Map> getTransationDetailList(@ApiParam(value = "单号") @RequestParam String tranid,
                                                       @ApiParam(value = "单据类型") @RequestParam(required = false) String transactionType,
                                                       @ApiParam(value = "波次里的订单号") @RequestParam(required = false) String orderNumber,
                                                       @ApiParam(value = "当前店铺id") @RequestParam(required = false) String location,
                                                       @ApiParam(value = "当前用户token") @RequestParam(required = false) String token,
                                                       @ApiParam(value = "用户Id") @RequestParam(required = false) String userId,
                                                       @ApiParam(value = "入口") @RequestParam(required = false) String entry
    ) {

        Map<String, Object> authonMap = AuthonTokenUtil.checkToken(token, userId);
        System.out.println(authonMap);
        User currentUser = null;
        String accountId = null;
        if ((authonMap != null) && ((Constant.NO).equals(authonMap.get("isAvailable")))) {
            return ResponseEntity.ok(authonMap);
        } else {
            currentUser = userService.getUserInfoByToken(token);
            if (currentUser == null) {
                authonMap.put("isAvailable", Constant.NO);
                authonMap.put("errorCode", Constant.TOKEN_ERRCODE_EXPIRE);
                authonMap.put("msg", "签名过期");
                return ResponseEntity.ok(authonMap);
            }
            accountId = currentUser.getAccountId();
        }
        Map<String, Object> map = transactionsService.processTransation(tranid, transactionType, orderNumber, location, accountId, entry);
        return ResponseEntity.ok(map);
    }

    @ApiOperation(value = "提交单据行数据更新")
    @RequestMapping(value = "/saveTransationDetailLine", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Map> saveTransationDetailLine(@RequestBody Map body) {
        String token = (String) body.get("token");
        String userId = body.get("userId").toString();
        String transactionType = (String) body.get("transactionType");
        Map<String, Object> authonMap = AuthonTokenUtil.checkToken(token, userId);
        System.out.println(authonMap);
        User currentUser;
        if ((authonMap != null) && ((Constant.NO).equals(authonMap.get("isAvailable")))) {
            return ResponseEntity.ok(authonMap);
        } else {
            currentUser = userService.getUserInfoByToken(token);
            if (currentUser == null) {
                authonMap.put("isAvailable", Constant.NO);
                authonMap.put("errorCode", Constant.TOKEN_ERRCODE_EXPIRE);
                authonMap.put("msg", "签名过期");
                return ResponseEntity.ok(authonMap);
            } else {
                body.put("accountId", currentUser.getAccountId());
            }
        }

        if (!"INVENTORY_COUNT".equals(transactionType) && !"NO_INVENTORY_COUNT".equals(transactionType)) {
            Map<String, Object> param = new HashMap(16);
            param.put("accountId", currentUser.getAccountId());
            param.put("location", currentUser.getLocationId());
            param.put("itemId", body.get("itemId"));
            param.put("binNumber", body.get("binNumber"));
            //param.put("lotNumber", body.get("lotNumber"));
            List<Map<String, Object>> list = transactionsService.searchICItem(param);
            if (list.size() > 0) {
                Map<String, Object> map = new HashMap(16);
                map.put("result", true);
                map.put("status", CommonEnum.value("eFail"));
                map.put("msg", "This SKU is now being counted, do not operate!");
                return ResponseEntity.ok(map);
            }
        }

        if ("TRACKING_NUMBER".equals(transactionType) || "PO".equals(transactionType) || "TO".equals(transactionType)
                || "ITEM_RECEIPT_PLAN".equals(transactionType) || "SO_RETURN".equals(transactionType) || "PO_RETURN".equals(transactionType)) {
            body.put("binNumber", "QC BIN");
        } else if ("BIN_TRANSFER_TASK".equals(transactionType) || "BIN_TRANSFER".equals(transactionType)) {
            body.put("to_bin_number", body.get("binNumber"));
            body.put("binNumber", "PUT ON BIN");
        } else if ("TO_WAVE".equals(transactionType) || "SO_WAVE".equals(transactionType)) {
            body.put("binNumber", body.get("binNumber"));
            body.put("to_bin_number", "PICK ON BIN");
        } else if ("INVENTORY_COUNT".equals(transactionType)) {
            body.put("binNumber", body.get("binNumber"));
        }
        return transactionInventoryDetailsService.saveTransationDetailLine(body, currentUser.getUserName());
    }

    @ApiOperation(value = "提交单据")
    @RequestMapping(value = "/submitTransaction", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Map> submitTransaction(@RequestBody Map body) {
        String token = (String) body.get("token");
        String userId = body.get("userId").toString();
        Map<String, Object> authonMap = AuthonTokenUtil.checkToken(token, userId);
        User currentUser;
        if ((authonMap != null) && ((Constant.NO).equals(authonMap.get("isAvailable")))) {
            return ResponseEntity.ok(authonMap);
        } else {
            currentUser = userService.getUserInfoByToken(token);
            if (currentUser == null) {
                authonMap.put("isAvailable", Constant.NO);
                authonMap.put("errorCode", Constant.TOKEN_ERRCODE_EXPIRE);
                authonMap.put("msg", "签名过期");
                return ResponseEntity.ok(authonMap);
            } else {
                body.put("accountId", currentUser.getAccountId());
                body.put("location", currentUser.getLocationId());
            }
        }
        return transactionsService.submitTransaction(body, currentUser);
    }

    @ApiOperation(value = "小包出库")
    @RequestMapping(value = "/submitTrackingNumber", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Map> submitTrackingNumber(@RequestBody Map body) {
        String token = (String) body.get("token");
        /*String userId = (String) body.get("userId");*/
        System.out.println("得到的user ID = " + body.get("userId"));
        String userId = String.valueOf(body.get("userId"));
        System.out.println("转化后的user ID = " + userId);
        Map<String, Object> authonMap = AuthonTokenUtil.checkToken(token, userId);
        User currentUser;
        if ((authonMap != null) && ((Constant.NO).equals(authonMap.get("isAvailable")))) {
            return ResponseEntity.ok(authonMap);
        } else {
            currentUser = userService.getUserInfoByToken(token);
            if (currentUser == null) {
                authonMap.put("isAvailable", Constant.NO);
                authonMap.put("errorCode", Constant.TOKEN_ERRCODE_EXPIRE);
                authonMap.put("msg", "签名过期");
                return ResponseEntity.ok(authonMap);
            } else {
                body.put("accountId", currentUser.getAccountId());
                body.put("location", currentUser.getLocationId());
                body.put("username", currentUser.getUserName());
            }
        }
        return transactionsService.submitTrackingNumber(body);
    }

    @ApiOperation(value = "箱唛入库")
    @RequestMapping(value = "/submitToShippingMark", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Map> submitToShippingMark(@RequestBody Map body) {
        String token = (String) body.get("token");
        String userId = (String) body.get("userId");
        Map<String, Object> authonMap = AuthonTokenUtil.checkToken(token, userId);
        User currentUser;
        if ((authonMap != null) && ((Constant.NO).equals(authonMap.get("isAvailable")))) {
            return ResponseEntity.ok(authonMap);
        } else {
            currentUser = userService.getUserInfoByToken(token);
            if (currentUser == null) {
                authonMap.put("isAvailable", Constant.NO);
                authonMap.put("errorCode", Constant.TOKEN_ERRCODE_EXPIRE);
                authonMap.put("msg", "签名过期");
                return ResponseEntity.ok(authonMap);
            } else {
                body.put("accountId", currentUser.getAccountId());
                body.put("location", currentUser.getLocationId());
            }
        }
        return transactionsService.submitToShippingMark(body);
    }

    @ApiOperation(value = "大货出库清单")
    @RequestMapping(value = "/getTransationItemST", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Map> getTransationST(@ApiParam(value = "单号") @RequestParam String tranid,
                                               @ApiParam(value = "箱唛") @RequestParam(required = false) String orderNumber,
                                               @ApiParam(value = "当前用户token") @RequestParam(required = false) String token,
                                               @ApiParam(value = "用户Id") @RequestParam(required = false) String userId
    ) {
        Map<String, Object> authonMap = AuthonTokenUtil.checkToken(token, userId);
        System.out.println(authonMap);
        User currentUser = null;
        String accountId = null;
        if ((authonMap != null) && ((Constant.NO).equals(authonMap.get("isAvailable")))) {
            return ResponseEntity.ok(authonMap);
        } else {
            currentUser = userService.getUserInfoByToken(token);
            if (currentUser == null) {
                authonMap.put("isAvailable", Constant.NO);
                authonMap.put("errorCode", Constant.TOKEN_ERRCODE_EXPIRE);
                authonMap.put("msg", "签名过期");
                return ResponseEntity.ok(authonMap);
            }
            accountId = currentUser.getAccountId();
        }
        Map<String, Object> map = transactionsService.selectSTList(tranid, orderNumber, accountId);
        return ResponseEntity.ok(map);
    }

    @ApiOperation(value = "大货出库")
    @RequestMapping(value = "/submitST", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Map> submitST(@RequestBody Map body) {
        String token = (String) body.get("token");
        String userId = (String) body.get("userId");
        String tranid = (String) body.get("tranid");
        String transType = (String) body.get("transType");
        Map<String, Object> authonMap = AuthonTokenUtil.checkToken(token, userId);
        User currentUser;
        String accountId = null;
        if ((authonMap != null) && ((Constant.NO).equals(authonMap.get("isAvailable")))) {
            return ResponseEntity.ok(authonMap);
        } else {
            currentUser = userService.getUserInfoByToken(token);
            Map<String, Object> map1 = new HashMap(16);
            map1.put("tranid", tranid);
            map1.put("accountId", currentUser.getAccountId());
            map1.put("username", currentUser.getUserName());
            transactionsService.updateUserByTranid(map1);
            if (currentUser == null) {
                authonMap.put("isAvailable", Constant.NO);
                authonMap.put("errorCode", Constant.TOKEN_ERRCODE_EXPIRE);
                authonMap.put("msg", "签名过期");
                return ResponseEntity.ok(authonMap);
            }
            accountId = currentUser.getAccountId();
        }
        WmsThread wms = new WmsThread("wms-mq", tranid, transType, producer, "ST_BIG", accountId);
        wms.start();
        Map<String, Object> map = new HashMap(16);
        map.put("status", CommonEnum.value("eSuccess"));
        map.put("msg", CommonEnum.value("eSMessage"));
        return ResponseEntity.ok(map);
    }

    @ApiOperation(value = "拣选框清单")
    @RequestMapping(value = "/getWavePickUp", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Map> getWavePickUp(@ApiParam(value = "单号") @RequestParam String tranid,
                                             @ApiParam(value = "拣选框") @RequestParam(required = false) String pickup,
                                             @ApiParam(value = "当前用户token") @RequestParam(required = false) String token,
                                             @ApiParam(value = "用户Id") @RequestParam(required = false) String userId
    ) {
        Map<String, Object> authonMap = AuthonTokenUtil.checkToken(token, userId);
        System.out.println(authonMap);
        User currentUser = null;
        String accountId = null;
        int location;
        if ((authonMap != null) && ((Constant.NO).equals(authonMap.get("isAvailable")))) {
            return ResponseEntity.ok(authonMap);
        } else {
            currentUser = userService.getUserInfoByToken(token);
            if (currentUser == null) {
                authonMap.put("isAvailable", Constant.NO);
                authonMap.put("errorCode", Constant.TOKEN_ERRCODE_EXPIRE);
                authonMap.put("msg", "签名过期");
                return ResponseEntity.ok(authonMap);
            }
            accountId = currentUser.getAccountId();
            location = currentUser.getLocationId();
        }
        Map<String, Object> map = transactionsService.selectWavePickUp(tranid, pickup, accountId, location);
        return ResponseEntity.ok(map);
    }

    @ApiOperation(value = "提交拣选框")
    @RequestMapping(value = "/submitWavePickUp", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Map> submitWavePickUp(@RequestBody Map body) {
        String token = (String) body.get("token");
        String userId = (String) body.get("userId");
        String tranid = (String) body.get("tranid");
        String transType = (String) body.get("transType");
        Map<String, Object> authonMap = AuthonTokenUtil.checkToken(token, userId);
        User currentUser;
        String accountId = null;
        if ((authonMap != null) && ((Constant.NO).equals(authonMap.get("isAvailable")))) {
            return ResponseEntity.ok(authonMap);
        } else {
            currentUser = userService.getUserInfoByToken(token);
            if (currentUser == null) {
                authonMap.put("isAvailable", Constant.NO);
                authonMap.put("errorCode", Constant.TOKEN_ERRCODE_EXPIRE);
                authonMap.put("msg", "签名过期");
                return ResponseEntity.ok(authonMap);
            }
            accountId = currentUser.getAccountId();
        }
        WmsThread wms = new WmsThread("wms-mq", tranid, transType, producer, "SO_WAVE", accountId);
        wms.start();
        Map<String, Object> map = new HashMap(16);
        map.put("status", CommonEnum.value("eSuccess"));
        map.put("msg", CommonEnum.value("eSMessage"));
        return ResponseEntity.ok(map);
    }

    @ApiOperation(value = "验证物流规则")
    @RequestMapping(value = "/getShippingCarrier", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Map> getShippingCarrier(@ApiParam(value = "单号") @RequestParam String trackingNumber,
                                                  @ApiParam(value = "当前用户token") @RequestParam(required = false) String token,
                                                  @ApiParam(value = "用户Id") @RequestParam(required = false) String userId
    ) {
        Map<String, Object> authonMap = AuthonTokenUtil.checkToken(token, userId);
        System.out.println(authonMap);
        User currentUser = null;
        String accountId = null;
        if ((authonMap != null) && ((Constant.NO).equals(authonMap.get("isAvailable")))) {
            return ResponseEntity.ok(authonMap);
        } else {
            currentUser = userService.getUserInfoByToken(token);
            if (currentUser == null) {
                authonMap.put("isAvailable", Constant.NO);
                authonMap.put("errorCode", Constant.TOKEN_ERRCODE_EXPIRE);
                authonMap.put("mtuisg", "签名过期");
                return ResponseEntity.ok(authonMap);
            }
            accountId = currentUser.getAccountId();
        }
        Map<String, Object> map = transactionsService.getShippingCarrier(trackingNumber, accountId);
        return ResponseEntity.ok(map);
    }

	/*@ApiOperation(value = "盘点")
	@RequestMapping(value = "/getInventoryCount", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Map> getInventoryCount(@ApiParam(value = "单号") @RequestParam String tranid,
											   @ApiParam(value = "商品") @RequestParam(required = false) int itemId,
											   @ApiParam(value = "当前用户token") @RequestParam(required = false) String token,
											   @ApiParam(value = "用户Id") @RequestParam(required = false) String userId
	) {
		Map<String, Object> authonMap = AuthonTokenUtil.checkToken(token, userId);
		System.out.println(authonMap);
		User currentUser = null;
		String accountId = null;
		int location=0;
		if ((authonMap != null) && ((Constant.NO).equals(authonMap.get("isAvailable")))) {
			return ResponseEntity.ok(authonMap);
		} else {
			currentUser = userService.getUserInfoByToken(token);
			if (currentUser == null) {
				authonMap.put("isAvailable", Constant.NO);
				authonMap.put("errorCode", Constant.TOKEN_ERRCODE_EXPIRE);
				authonMap.put("msg", "签名过期");
				return ResponseEntity.ok(authonMap);
			}
			accountId = currentUser.getAccountId();
			location = currentUser.getLocationId();
		}
		Map<String, Object> map =transactionsService.processTransation(tranid,transactionType,location,accountId);
		return ResponseEntity.ok(map);
	}*/


    @ApiOperation(value = "查询库位下已扫描的数量")
    @RequestMapping(value = "/getScanedNumBybin", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Map> getScanedNumBybin(@ApiParam(value = "单号") @RequestParam String tranid,
                                                 @ApiParam(value = "商品ID") @RequestParam int itemid,
                                                 @ApiParam(value = "当前用户token") @RequestParam(required = false) String token,
                                                 @ApiParam(value = "用户Id") @RequestParam(required = false) String userId
    ) {

        Map<String, Object> authonMap = AuthonTokenUtil.checkToken(token, userId);
        User currentUser = null;
        if ((authonMap != null) && ((Constant.NO).equals(authonMap.get("isAvailable")))) {
            return ResponseEntity.ok(authonMap);
        } else {
            currentUser = userService.getUserInfoByToken(token);
            if (currentUser == null) {
                authonMap.put("isAvailable", Constant.NO);
                authonMap.put("errorCode", Constant.TOKEN_ERRCODE_EXPIRE);
                authonMap.put("msg", "签名过期");
                return ResponseEntity.ok(authonMap);
            }
        }
        return transactionInventoryDetailsService.getScanedNumBybin(tranid, itemid);
    }


    @ApiOperation(value = "销售出库清单V2")
    @RequestMapping(value = "/getTxSubmitDetail2", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Map> getTxSubmitDetail2(@ApiParam(value = "单号") @RequestParam String tranid,
                                                  @ApiParam(value = "单据类型") @RequestParam(required = false) String transactionType,
                                                  @ApiParam(value = "当前用户token") @RequestParam(required = false) String token,
                                                  @ApiParam(value = "用户Id") @RequestParam(required = false) String userId
    ) {
        Map<String, Object> authonMap = AuthonTokenUtil.checkToken(token, userId);
        System.out.println(authonMap);
        User currentUser = null;
        String accountId = null;
        if ((authonMap != null) && ((Constant.NO).equals(authonMap.get("isAvailable")))) {
            return ResponseEntity.ok(authonMap);
        } else {
            currentUser = userService.getUserInfoByToken(token);
            if (currentUser == null) {
                authonMap.put("isAvailable", Constant.NO);
                authonMap.put("errorCode", Constant.TOKEN_ERRCODE_EXPIRE);
                authonMap.put("msg", "签名过期");
                return ResponseEntity.ok(authonMap);
            }
            accountId = currentUser.getAccountId();
        }
        Map<String, Object> map = transactionsService.getTxSubmitDetail2(tranid, transactionType, accountId);
        return ResponseEntity.ok(map);
    }

    @ApiOperation(value = "库存调整单查找item")
    @RequestMapping(value = "/searchItem", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Map> searchItem(@ApiParam(value = "当前用户token") @RequestParam String token,
                                          @ApiParam(value = "用户Id") @RequestParam String userId,
                                          @ApiParam(value = "itemName") @RequestParam String itemName
    ) {
        Map<String, Object> authonMap = AuthonTokenUtil.checkToken(token, userId);
        System.out.println(authonMap);
        User currentUser = null;
        String accountId = null;
        if ((authonMap != null) && ((Constant.NO).equals(authonMap.get("isAvailable")))) {
            return ResponseEntity.ok(authonMap);
        } else {
            currentUser = userService.getUserInfoByToken(token);
            if (currentUser == null) {
                authonMap.put("isAvailable", Constant.NO);
                authonMap.put("errorCode", Constant.TOKEN_ERRCODE_EXPIRE);
                authonMap.put("msg", "You have been logged out");
                return ResponseEntity.ok(authonMap);
            }
            accountId = currentUser.getAccountId();
        }
        Map<String, Object> param = new HashMap(16);
        param.put("itemName", itemName);
        param.put("accountId", accountId);
        Items itemInfo = itemService.getItemInfo(param);
        Map<String, Object> map = new HashMap(16);
        if (null != itemInfo) {
            map.put("result", itemInfo);
            map.put("status", CommonEnum.value("eSuccess"));
            map.put("msg", CommonEnum.value("eSMessage"));
        } else {
            map.put("status", CommonEnum.value("eNotFind"));
            map.put("msg", CommonEnum.value("notFind"));
        }
        return ResponseEntity.ok(map);
    }

    @ApiOperation(value = "创建库存调整单")
    @RequestMapping(value = "/creatInventoryAdjustment", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Map> creatInventoryAdjustment(@ApiParam(value = "请求体") @RequestBody(required = false) Map request) {
        String token = request.get("token").toString();
        Map<String, Object> authonMap = AuthonTokenUtil.checkToken(token, request.get("userId").toString());
        User currentUser;
        if ((authonMap != null) && ((Constant.NO).equals(authonMap.get("isAvailable")))) {
            return ResponseEntity.ok(authonMap);
        } else {
            currentUser = userService.getUserInfoByToken(token);
            if (currentUser == null) {
                authonMap.put("isAvailable", Constant.NO);
                authonMap.put("errorCode", Constant.TOKEN_ERRCODE_EXPIRE);
                authonMap.put("msg", "You have been logged out");
                return ResponseEntity.ok(authonMap);
            } else {
                request.put("accountId", currentUser.getAccountId());
                request.put("location", currentUser.getLocationId());
                request.put("userName", currentUser.getUserName());
            }
        }
        return transactionsService.creatInventoryAdjustment(request);
    }

    @ApiOperation(value = "查找错误日志")
    @RequestMapping(value = "/searchErrorTransactions", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Map> searchErrorTransactions(@ApiParam(value = "当前用户token") @RequestParam String token,
                                                     @ApiParam(value = "用户Id") @RequestParam String userId,
                                                     @ApiParam(value = "单号") @RequestParam String tranid,
                                                     @ApiParam(value = "每页显示数量(true)") @RequestParam Integer pageSize,
                                                     @ApiParam(value = "页数(true)") @RequestParam Integer pageNum,
                                                     @ApiParam(value = "fromDate") @RequestParam String fromDate,
                                                     @ApiParam(value = "toDate") @RequestParam String toDate
    ) {
        Map<String, Object> authonMap = AuthonTokenUtil.checkToken(token, userId);
        System.out.println(authonMap);
        User currentUser = null;
        String accountId = null;
        String userName = null;
        int location = 0;
        if ((authonMap != null) && ((Constant.NO).equals(authonMap.get("isAvailable")))) {
            return ResponseEntity.ok(authonMap);
        } else {
            currentUser = userService.getUserInfoByToken(token);
            if (currentUser == null) {
                authonMap.put("isAvailable", Constant.NO);
                authonMap.put("errorCode", Constant.TOKEN_ERRCODE_EXPIRE);
                authonMap.put("msg", "You have been logged out");
                return ResponseEntity.ok(authonMap);
            }
            accountId = currentUser.getAccountId();
            location = currentUser.getLocationId();
            userName = currentUser.getUserName();
        }
        Map<String, Object> param = new HashMap(16);
        if (tranid!=null && tranid!=""){
            param.put("tranid", tranid);
        }
        param.put("accountId", accountId);
        param.put("userName", userName);
        param.put("fromDate", fromDate);
        param.put("toDate", toDate);
        param.put("pageSize", pageSize);
        param.put("pageNum", pageNum);
        List<Transactions> errorTransactions = transactionsService.searchErrorTransactions(param);
        Map<String, Object> map = new HashMap(16);
        if (null != errorTransactions && errorTransactions.size() > 0) {
            //logger.info("-------------产品分页查询成功并返回-----------------------" + errorTransactions.size() + "条");
            PageInfo<Transactions> pageInfo = new PageInfo<>(errorTransactions);
            if (null != pageInfo) {
                DataGridResult data = new DataGridResult();
                data.setRows(pageInfo.getList());
                data.setPageSize(pageInfo.getPageSize());
                data.setPageNum(pageInfo.getPageNum());
                data.setEndRow(pageInfo.getEndRow());
                data.setStartRow(pageInfo.getStartRow());
                data.setTotal(pageInfo.getTotal());
                map.put("size", pageInfo.getTotal()/pageInfo.getPageSize());
                map.put("result", data);
                map.put("status", CommonEnum.value("eSuccess"));
                map.put("msg", CommonEnum.value("eSMessage"));
            }
        } else {
            map.put("status", CommonEnum.value("eNotFind"));
            map.put("msg", CommonEnum.value("notFind"));

        }
        return ResponseEntity.ok(map);
    }

    @ApiOperation(value = "查找商品的出入库明细")
    @RequestMapping(value = "/searchItemOperateData", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Map> searchItemOperateData(@ApiParam(value = "当前用户token") @RequestParam String token,
                                                     @ApiParam(value = "用户Id") @RequestParam String userId,
                                                     @ApiParam(value = "商品") @RequestParam String item,
                                                     @ApiParam(value = "每页显示数量(true)") @RequestParam Integer pageSize,
                                                     @ApiParam(value = "页数(true)") @RequestParam Integer pageNum,
                                                     @ApiParam(value = "binName") @RequestParam String binName,
                                                     //@ApiParam(value = "toBinName") @RequestParam String toBinName,
                                                     @ApiParam(value = "fromDate") @RequestParam String fromDate,
                                                     @ApiParam(value = "toDate") @RequestParam String toDate
    ) {
        Map<String, Object> authonMap = AuthonTokenUtil.checkToken(token, userId);
        System.out.println(authonMap);
        User currentUser = null;
        String accountId = null;
        int location=0;
        if ((authonMap != null) && ((Constant.NO).equals(authonMap.get("isAvailable")))) {
            return ResponseEntity.ok(authonMap);
        } else {
            currentUser = userService.getUserInfoByToken(token);
            if (currentUser == null) {
                authonMap.put("isAvailable", Constant.NO);
                authonMap.put("errorCode", Constant.TOKEN_ERRCODE_EXPIRE);
                authonMap.put("msg", "You have been logged out");
                return ResponseEntity.ok(authonMap);
            }
            accountId = currentUser.getAccountId();
            location = currentUser.getLocationId();
        }
        Map<String, Object> param = new HashMap(16);
        param.put("itemName", item);
        param.put("accountId", accountId);
        param.put("location", location);
        param.put("pageSize", pageSize);
        param.put("pageNum", pageNum);
        param.put("binName", binName);
        //param.put("toBinName", toBinName);
        param.put("fromDate", fromDate);
        param.put("toDate", toDate);
        List<NSOperateData> nsOperateData = nsOperateDataService.searchItemOperateData(param);
        Map<String, Object> map = new HashMap(16);
        if (null != nsOperateData && nsOperateData.size() > 0) {
            //logger.info("-------------产品分页查询成功并返回-----------------------" + nsOperateData.size() + "条");
            PageInfo<NSOperateData> pageInfo = new PageInfo<>(nsOperateData);
            if (null != pageInfo) {
                DataGridResult data = new DataGridResult();
                data.setRows(pageInfo.getList());
                data.setPageSize(pageInfo.getPageSize());
                data.setPageNum(pageInfo.getPageNum());
                data.setEndRow(pageInfo.getEndRow());
                data.setStartRow(pageInfo.getStartRow());
                data.setTotal(pageInfo.getTotal());
                map.put("size", pageInfo.getTotal()/pageInfo.getPageSize());
                map.put("result", data);
                map.put("status", CommonEnum.value("eSuccess"));
                map.put("msg", CommonEnum.value("eSMessage"));
            }
        } else {
            map.put("status", CommonEnum.value("eNotFind"));
            map.put("msg", CommonEnum.value("notFind"));

        }
        return ResponseEntity.ok(map);
    }
	  @ApiOperation(value = "导出出入库明细")
    @RequestMapping(value = "/exportOperateData", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public void exportOperateData(@ApiParam(value = "当前用户token") @RequestParam String token,
                                  @ApiParam(value = "用户Id") @RequestParam String userId,
                                  @ApiParam(value = "商品") @RequestParam String item,
                                  @ApiParam(value = "每页显示数量(true)") @RequestParam Integer pageSize,
                                  @ApiParam(value = "页数(true)") @RequestParam Integer pageNum,
                                  @ApiParam(value = "binName") @RequestParam String binName,
                                  @ApiParam(value = "fromDate") @RequestParam String fromDate,
                                  @ApiParam(value = "toDate") @RequestParam String toDate, HttpServletResponse response
    ) {
        Map<String, Object> authonMap = AuthonTokenUtil.checkToken(token, userId);
        System.out.println(authonMap);
        User currentUser = null;
        String accountId = null;
        int location = 0;
        if ((authonMap != null) && ((Constant.NO).equals(authonMap.get("isAvailable")))) {
            return;
        }
        currentUser = userService.getUserInfoByToken(token);
        if (currentUser == null) {
            authonMap.put("isAvailable", Constant.NO);
            authonMap.put("errorCode", Constant.TOKEN_ERRCODE_EXPIRE);
            authonMap.put("msg", "You have been logged out");
            return;
        }
        accountId = currentUser.getAccountId();
        location = currentUser.getLocationId();

        pageNum = 0;
        pageSize = 100000;

        Map<String, Object> param = new HashMap(16);
        param.put("itemName", item);
        param.put("accountId", accountId);
        param.put("location", location);
        param.put("pageSize", pageSize);
        param.put("pageNum", pageNum);
        param.put("binName", binName);
        //param.put("toBinName", toBinName);
        param.put("fromDate", fromDate);
        param.put("toDate", toDate);
        List<NSOperateData> list = nsOperateDataService.searchItemOperateData(param);
        List<List<Object>> rows = new ArrayList(); //所有的行的数据
        ExcelData data = new ExcelData();
        data.setName("库位明细表"); //文件名称
        List<String> titles = new ArrayList(); //excel标题头
        titles.add("序号");
        titles.add("操作时间");
        titles.add("单据类型");
        titles.add("单据名称");
        titles.add("仓库名称");
        titles.add("SKU");
        titles.add("库位");
        titles.add("数量");

        data.setTitles(titles);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String fileName = "库位明细表"+sdf.format(new Date()) + ".xlsx";
        int index = 0 ;
        if (null != list && list.size() > 0) {
           //将数据放入二维数组中去
            for (int i = 0; i < list.size(); i++) {
                List<Object> row = new ArrayList(); //每一行数据
                NSOperateData cust = list.get(i);
                index++;
                row.add(index);
                row.add(sdf.format(cust.getCreationDate()));
                row.add(cust.getType());
                row.add(cust.getTranid());
                row.add(cust.getLocationName());
                row.add(cust.getItem());
                row.add(cust.getBinName());
                row.add(cust.getQuantity());
                rows.add(row);
            }
        }

        data.setRows(rows);
        try{
            ExcelUtils.exportExcel(response, fileName, data);
        }catch (Exception e){

        }

    }

}
